/*
 * LCTRemoteImpl.java
 *
 * Created on October 18, 2007, 1:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.lct.eq2.rmi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Properties;
import com.lct.eq2.data.*;
import com.lct.eq2.util.DailyLog;

/**
 *
 * @author waitek
 */
public class LCTRemoteImpl extends UnicastRemoteObject implements LCTRemote {
    private HashMap tentativeRespawns;
    private HashMap officialRespawns;
    private DailyLog logger;
    private static String path = "C:\\Documents and Settings\\Me\\Desktop\\Code\\LCT\\";
    
    /** Creates a new instance of LCTRemoteImpl */
    public LCTRemoteImpl() throws RemoteException {
        logger = DailyLog.getInstance("logs", "LCTServer");
        try {
            File t = new File(path + "tentativeRespawns.dat");
            File o = new File(path + "officialRespawns.dat");
            FileInputStream fis = new FileInputStream(t);
            ObjectInputStream ois = new ObjectInputStream(fis);
            tentativeRespawns = (HashMap)ois.readObject();
            fis = new FileInputStream(o);
            ois = new ObjectInputStream(fis);
            officialRespawns = (HashMap)ois.readObject();
        } catch (FileNotFoundException ex) {
            try {
                HashMap map = new HashMap();
                FileOutputStream fos = new FileOutputStream(path + "tentativeRespawns.dat");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                map.put("nameMap", new HashMap());
                oos.writeObject(map);
                fos.close();
                oos.close();
                fos = new FileOutputStream(path + "officialRespawns.dat");
                oos = new ObjectOutputStream(fos);
                oos.writeObject(map);
                fos.close();
                oos.close();
                tentativeRespawns = map;
                officialRespawns = new HashMap();
            } catch (FileNotFoundException exc) {
                exc.printStackTrace();
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public HashMap getTimers() {
        return officialRespawns;
    }

    public String getVersion() {
        String rval = null;
        Properties props = new Properties();
        String propFile = path + "lctserver.properties";
        logger.info("LCTRemoteImpl", "getVersion()", "Request made for version.");
        try {
            props.load(new FileInputStream(new File(propFile)));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        rval = props.getProperty("version");
        return rval;
    }
    
    public void addRespawnTimer(RespawnTimer timer, String clientID) {
//        if (timer == null) {
//            System.out.println("timer is null");
//        }

        String name = timer.getCombatantName();
        String duration = timer.getTotalDuration().getDuration();
        logger.info("LCTRemoteImpl", "addRespawnTimer", "Received new Respawn Timer from: " + clientID 
                + ". Timer name = " + name + ", duration = " + duration);
        System.out.println("Received new Respawn Timer: " + name + ", duration = " + timer.getTotalDuration().getDuration());
        if (!officialRespawns.containsKey(name)) {
            System.out.println("\tNo official respawn found.");
            HashMap nameMap = (HashMap)tentativeRespawns.get("nameMap");
            if (nameMap.containsKey(clientID)) {
                System.out.println("\t\tClient ID Found.");
                HashMap clientIDMap = (HashMap)tentativeRespawns.get(clientID);
                if (!clientIDMap.containsKey(name)) {
                    System.out.println("\t\t\tClient map doesn't contain " + name);
                    clientIDMap.put(name, timer);
                    nameMap.put(clientID, clientIDMap);
                    tentativeRespawns.put("nameMap", nameMap);
                }
            } else {
                System.out.println("\t\tClient ID NOT found, creating new.");
                HashMap clientIDMap = new HashMap();
                nameMap.put(clientID, clientIDMap);
                tentativeRespawns.put("nameMap", nameMap);
            }
            
            if (tentativeRespawns.containsKey(name + "::" + duration)) {
                System.out.println("\t" + name + "::" + duration + " found in tentativeRespawns");
                String toSplit = (String)tentativeRespawns.get(name + "::" + duration);
                System.out.println("\ttoSplit = " + toSplit);
                String[] split = toSplit.split("::");
                Integer tmp = Integer.parseInt(split[1]);
                System.out.println("\ttmp = " + tmp);
                if (tmp < 2) {
                    System.out.println("\tsplit[0]::tmp+1 = " + split[0] + "::" + (tmp + 1));
                    tentativeRespawns.put(name + "::" + duration, split[0] + "::" + (tmp + 1));
                } else {
                    tentativeRespawns.remove(name + "::" + duration);
                    officialRespawns.put(name, timer);
                    storeRespawns(officialRespawns, "officialRespawns.dat");
                    System.out.println("Timer is now official. Name = " + name + ", duration = " + duration);
                    logger.info("LCTRemoteImpl", "addRespawnTimer"
                            , "Timer is now official. Name = " + name + ", duration = " + duration);
                }
            } else {
                System.out.println("\tRespawn added to tentativeRespawns: " + name + "::" + duration);
                tentativeRespawns.put(name + "::" + duration, duration + "::1");
            }
            storeRespawns(tentativeRespawns, "tentativeRespawns.dat");
        }
    }
    
    private void storeRespawns(HashMap map, String mapName) {
        try {
            FileOutputStream fos = new FileOutputStream(new File(path + mapName));
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            fos.close();
            oos.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
}
